//Copyright 2011 New York City 3D Community

//This file is part of New York City 3D.

//New York City 3D is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//New York City 3D is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with New York City 3D.  If not, see <http://www.gnu.org/licenses/>.

package nyc3d.street;

import com.jme3.asset.AssetManager;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import nyc3d.NYC3D;

public class Streetlight {
    String name;
    Node parentNode, rootNode;
    Spatial spatial;
    Material material;
    PointLight light;
    public enum Type{COBRA_HEAD};
    Type type;
    StreetlightBase base;
    boolean lit;

    public Streetlight(String name, Type type, AssetManager assetManager, Vector3f translation, Vector3f rotation, Vector3f baseRotation, Vector3f scale, Node parentNode, Node rootNode){
        this.name = name;
        this.parentNode = parentNode;
        this.type = type;
        this.rootNode = rootNode;
        lit = true;

        switch(type){
            case COBRA_HEAD:
                spatial = assetManager.loadModel("Models/CobraHeadStreetlight/CobraHeadStreetlight.j3o");
                break;
        }        

        Point2D original = new Point2D.Float(translation.x + 8.99f, translation.z);
        AffineTransform at = AffineTransform.getRotateInstance(-rotation.y, translation.x, translation.z);
        Point2D rotated = at.transform(original, null);

        light = new PointLight();
        light.setPosition(new Vector3f((float)rotated.getX(), translation.y + 31.5f, (float) rotated.getY()));
        //light.setPosition(spatial.getLocalTranslation());
        light.setRadius(50);
        light.setColor(ColorRGBA.White);

        material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        material.setFloat("Shininess", Float.MAX_VALUE);
        spatial.setMaterial(material);

        spatial.setLocalTranslation(translation);
        spatial.rotate(rotation.x, rotation.y, rotation.z);
        spatial.scale(scale.x, scale.y, scale.z);

        spatial.setShadowMode(ShadowMode.Cast);

        parentNode.attachChild(spatial);
        rootNode.addLight(light);

        base = new StreetlightBase(name + "_Base", StreetlightBase.Type.valueOf(type.toString()), assetManager, translation, baseRotation, scale, parentNode, rootNode, this);
    }

    public void update(){         
        if (NYC3D.day && lit){
             rootNode.removeLight(light);
             lit = false;
        }

        if (!NYC3D.day && !lit){
            rootNode.addLight(light);
            lit = true;
        }
    }

     public PointLight getLight() {
        return light;
    }

    public void setLight(PointLight light) {
        this.light = light;
    }

    public Material getMaterial() {
        return material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getParentNode() {
        return parentNode;
    }

    public void setParentNode(Node parentNode) {
        this.parentNode = parentNode;
    }

    public Spatial getSpatial() {
        return spatial;
    }

    public void setSpatial(Spatial spatial) {
        this.spatial = spatial;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public StreetlightBase getBase() {
        return base;
    }

    public void setBase(StreetlightBase base) {
        this.base = base;
    }

    public boolean isLit() {
        return lit;
    }

    public void setLit(boolean lit) {
        this.lit = lit;
    }

    public Node getRootNode() {
        return rootNode;
    }

    public void setRootNode(Node rootNode) {
        this.rootNode = rootNode;
    }
}